<?php
namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Apply;
use App\Models\Student;
use App\Models\Student_lost;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Encore\Admin\Grid;
use App\Models\Squad;
use App\Models\Term;
use App\Admin\Extensions\ExcelExpoter;

class ApplyController extends Controller
{
    private $class;

    public function __construct(Apply $apply)
    {
        $this->class = $apply;
    }

    public function get(Request $request)
    {
        return $this->class->get($request);
    }

    public function submit(Request $request)
    {
        $num = DB::table('applys')->where('student_id',$request['id'])->where('status','=','0')->count();
        if($num > 0){
            return array('status'=>-1,'message'=>'该生仍有其它未处理申请，请检查！');
        }
        $data = array();
        $data['type'] = $request['type'];
        $data['status'] = '0';
        $data['student_id'] = $request['id'];
        $data['trans_squad_id']  = isset($request['squad'])?$request['squad']:NULL;
        $data['op_id'] = Admin::user()->id;
        $data['squad_id'] = Student::find($request['id'])->squad_id;

        //add by Don at 2019-03-22 19点11分 start
        //$data['study_jindu'] = Squad::find(Student::find($request['id'])->squad_id)->study_jindu;
        //add by Don at 2019-03-22 19点11分 end

        $data['memo'] = $request['memo'];
        $data['created_at'] = date('Y-m-d H:i:s');

        $result = DB::table('applys')->insert($data);

        return array('status'=>1,'message'=>'操作成功');
    }

    public function my_apply(Content $content,Request $request)
    {
        $content->header('我提交的申请')
            ->description('列表')
            ->body($this->my_apply_grid($request))->body($this->my_apply_view());
        Admin::script($this->my_apply_script());
        return $content;
    }

    protected function my_apply_grid()
    {
        return Admin::grid(Apply::class, function (Grid $grid) {
            $user = Admin::user()->id;
            $stat = 0;
            $grid->model()->where('op_id', '=', $user)->orderBy('id','DSC');
            //$grid->attendance_status(trans('admin.status'))->editable('select', ['1' => '正常', '2' => '迟到','3' => '请假', '4' => '缺勤'])->sortable();

            $grid->student_id('学号')->display(function($student_id){
                return Student::find($student_id)->no;
            });
            $grid->student()->ch_name('中文名');
            $grid->student()->en_name('英文名');
            $grid->student()->sex('性别')->display(function($sex){
                if($sex == 1){
                    return '男';
                }elseif($sex == 2){
                    return '女';
                }
            });
            $grid->type('申请类型')->display(function($type){
                if($type == '1'){
                    return '转班';
                }elseif($type == '2'){
                    return '休学';
                }elseif($type == '3'){
                    return '退学';
                }elseif($type == '4'){
                    return '复学';
                }
            });
            $grid->student()->enrollment_time('入学时间')->display(function($enrollment_time){
                return date('Y-m-d',strtotime($enrollment_time));
            });
            $grid->squad_id('原班级')->display(function($squad_id){
                return Squad::find($squad_id)->name;
            });
            $grid->trans_squad_id('转入班级')->display(function($trans_squad_id){
                if(is_null($this->trans_squad_id) || $this->trans_squad_id == 0){
                    return '';
                }else{
                    return Squad::find($this->trans_squad_id)->name;
                }
            });
            $grid->op_id('发起人')->display(function($op_id){
                $temp = DB::table('admin_users')->where('id',$op_id)->get(['en_name']);
                foreach($temp as $v){
                    return $v->en_name;
                }
            });
            $grid->created_at('发起时间')->display(function($created_at){
                return date('Y-m-d',strtotime($created_at));
            });
            $grid->sp_id('审批人')->display(function($sp_id){
                $temp = DB::table('admin_users')->where('id',$sp_id)->get(['en_name']);
                foreach($temp as $v){
                    return $v->en_name;
                }
            });
            $grid->sp_time('处理时间')->display(function($sp_time){
                if(is_null($sp_time)){
                    return '';
                }else{
                    return date('Y-m-d',strtotime($sp_time));
                }
            });
            $grid->memo('备注');
            $grid->status('状态')->display(function($status) use($stat){
                $stat = $status;
                if($status == '0'){
                    return '未审核';
                }elseif($status == '1'){
                    return '审核通过';
                }elseif($status == '2'){
                    return '驳回';
                }
            });

            $grid->actions(function ($actions) {
                $actions->disableView();
                $actions->disableEdit();
                $actions->disableDelete();
                $stat = DB::table('applys')->where('id',$actions->getKey())->get();
                foreach($stat as $v){
                    if($v->status == '0' && $v->type == '1'){
                        $actions->append('<button class="btn btn-primary" onclick="trans('.$actions->getKey().')">编辑</button><button class="btn btn-danger" onclick="roll_back('.$actions->getKey().')">撤回</button>');
                    }elseif($v->status == '0' && in_array($v->type,['2','3'])){
                        $actions->append('<button class="btn btn-danger" onclick="roll_back('.$actions->getKey().')">撤回</button>');
                    }
                }
            });
            $grid->disableExport();
            $grid->disableCreateButton();

            $grid->tools(function($tools){
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });


            $grid->filter(function ($filter) {
                $filter->column(1/2, function ($filter) {
                    $filter->disableIdFilter();
                    $filter->equal('student.no', '学号');
                    $filter->equal('type', '类型')->select(['1'=>'转班','2'=>'休学','3'=>'退学','4'=>'复学']);
                });
                $filter->column(1/2, function ($filter) {
                    $filter->like('student.en_name','英文名');
                    $filter->equal('status', '状态')->select(['0'=>'未审核','1'=>'审核通过','2'=>'驳回']);
                });
            });
        });
    }

    protected function my_apply_script()
    {
        return <<<SCRIPT

        roll_back=function(id){
            $.confirm({
                title: '提示信息!',
                content: '确定要撤回申请吗？',
                buttons: {
                    ok: {
                        text: "确定",
                        btnClass: 'btn-danger',
                        keys: ['enter'],
                        action: function(){
                            $.ajax({
                                method: 'post',
                                url: 'delete',
                                data: {
                                    _token:LA.token,
                                    id: id
                                },
                                success: function () {
                                    $.pjax.reload('#pjax-container');
                                    toastr.success('撤回成功');
                                    parent.location.reload();
                                }
                            });
                        }
                    },
                    cancel: {
                        text: "取消",
                        btnClass: 'btn-primary',
                        keys: ['esc'],
                        action:function () {
                            $.pjax.reload('#pjax-container');
                        }
                    }
                }
            });
        }
SCRIPT;

    }

    protected function my_apply_view()
    {
        return view('admin.my_apply');
    }

    public function apply_list(Content $content,Request $request)
    {
        $content->header('学员变更')
            ->description('列表')
            ->body($this->apply_list_grid($request));
        return $content;
    }

    protected function apply_list_grid($request)
    {
        return Admin::grid(Apply::class, function (Grid $grid) use ($request) {
            $user = Admin::user()->id;
            $time = date('Y-m-d H:i:s');
            if($request['rtype'] == 'click'){
                //更新点击时间
                $num = DB::table('apply_click_time')->where('op_id',$user)->count();
                if($num > 0){
                    //存在则更新
                    DB::table('apply_click_time')->where('op_id',$user)->update(['op_time'=>$time]);
                }else{
                    //不存在则插入
                    DB::table('apply_click_time')->where('op_id',$user)->insert(['op_time'=>$time,'op_id'=>$user]);
                }
            }
            if(Admin::user()->isRole('apply_manager') || Admin::user()->isRole('德育部中层附加权限菜单') || Admin::user()->isRole('超级管理员')){
                $grid->model()->orderBy('id','DSC');
            }else{
                $squads = DB::table('squads')->where('class_teacher','=',$user)->get(['id']);
                $id_arr = array();
                foreach($squads as $c){
                    $id_arr[] = $c->id;
                }
                $grid->model()->where(function($builder) use ($id_arr){
                    $builder->whereIn('squad_id',$id_arr);
                    $builder->orWhereIn('trans_squad_id',$id_arr);
                })->orderBy('id','DSC');
            }
            //$grid->attendance_status(trans('admin.status'))->editable('select', ['1' => '正常', '2' => '迟到','3' => '请假', '4' => '缺勤'])->sortable();

            $grid->student_id('学号')->display(function($student_id){
                $stu = Student::find($this->student_id);
                return Student::find($this->student_id)->no;
            });
            //$grid->student()->ch_name('中文名');
            $grid->column('中文名')->display(function(){
                return Student::find($this->student_id)->ch_name;
            });
            //$grid->student()->en_name('英文名');
            $grid->column('英文名')->display(function(){
                return Student::find($this->student_id)->en_name;
            });
/*            $grid->student()->sex('性别')->display(function($sex){
                if($this->sex == 1){
                    return '男';
                }elseif($this->sex == 2){
                    return '女';
                }
            });*/
            $grid->column('性别')->display(function(){
                $sex = Student::find($this->student_id)->sex;
                if($sex == 1){
                    return '男';
                }elseif($sex == 2){
                    return '女';
                }
            });
            $grid->type('申请类型')->display(function($type){
                if($this->type == '1'){
                    return '转班';
                }elseif($this->type == '2'){
                    return '休学';
                }elseif($this->type == '3'){
                    return '退学';
                }elseif($this->type == '4'){
                    return '复学';
                }
            });
/*            $grid->student()->enrollment_time('入学时间')->display(function($enrollment_time){
                return date('Y-m-d',strtotime($this->enrollment_time));
            });*/
            $grid->column('入学时间')->display(function(){
                $time = Student::find($this->student_id)->sex;
                return date('Y-m-d',strtotime($time));
            });
            $grid->squad_id('原班级')->display(function($squad_id){
                return Squad::find($this->squad_id)->name;
            });
            $grid->trans_squad_id('转入班级')->display(function($trans_squad_id){
                if(is_null($this->trans_squad_id) || $this->trans_squad_id == 0){
                    return '';
                }else{
                    return Squad::find($this->trans_squad_id)->name;
                }
            });
            $grid->op_id('发起人')->display(function($op_id){
                $temp = DB::table('admin_users')->where('id',$this->op_id)->get(['en_name']);
                foreach($temp as $v){
                    return $v->en_name;
                }
            });
            $grid->created_at('发起时间')->display(function($created_at){
                return date('Y-m-d',strtotime($this->created_at));
            });
            $grid->sp_id('审批人')->display(function($sp_id){
                $temp = DB::table('admin_users')->where('id',$this->sp_id)->get(['en_name']);
                foreach($temp as $v){
                    return $v->en_name;
                }
            });
            $grid->sp_time('处理时间')->display(function($sp_time){
                if(is_null($this->sp_time)){
                    return '';
                }else{
                    return date('Y-m-d',strtotime($this->sp_time));
                }
            });
            $grid->memo('备注');
            $grid->status('状态')->display(function($status){
                if($this->status == '0'){
                    return '未审核';
                }elseif($this->status == '1'){
                    return '审核通过';
                }elseif($this->status == '2'){
                    return '驳回';
                }
            });

            $grid->exporter(new ExcelExpoter($grid,'学员变更列表导出'));
            $grid->disableActions();
            //$grid->disableExport();
            $grid->disableCreateButton();


            $grid->filter(function ($filter) {
                $filter->column(1/2, function ($filter) {
                    $filter->disableIdFilter();
                    $filter->equal('student.no', '学号');
                    $filter->in('type', '类型')->multipleSelect(['1'=>'转班','2'=>'休学','3'=>'退学','4'=>'复学']);
                    $filter->between('sp_time','操作时间')->datetime();
                });
                $filter->column(1/2, function ($filter) {
                    $filter->like('student.en_name','英文名');
                    $filter->equal('status', '状态')->select(['0'=>'未审核','1'=>'审核通过','2'=>'驳回']);
                });
            });
        });
    }

    public function sp_apply(Content $content)
    {
        $content->header('待我审批')
            ->description('列表')
            ->body($this->sp_apply_grid())->body($this->my_apply_view());
        Admin::script($this->sp_apply_script());
        return $content;
    }

    protected function sp_apply_grid()
    {
        return Admin::grid(Apply::class, function (Grid $grid) {
            if(Admin::user()->isRole('sp1') && !Admin::user()->isRole('sp2')){
                $grid->model()->where('status','=','0')->where('type','=','1');
            }elseif(Admin::user()->isRole('sp2') && !Admin::user()->isRole('sp1')){
                $grid->model()->where('status','=','0')->whereIn('type',['2','3']);
            }else{
                $grid->model()->where('status','=','0');
            }
            //$grid->attendance_status(trans('admin.status'))->editable('select', ['1' => '正常', '2' => '迟到','3' => '请假', '4' => '缺勤'])->sortable();

            $grid->student_id('学号')->display(function($student_id){
                return Student::find($student_id)->no;
            });
            $grid->student()->ch_name('中文名');
            $grid->student()->en_name('英文名');
            $grid->student()->sex('性别')->display(function($sex){
                if($sex == 1){
                    return '男';
                }elseif($sex == 2){
                    return '女';
                }
            });
            $grid->type('申请类型')->display(function($type){
                if($type == '1'){
                    return '转班';
                }elseif($type == '2'){
                    return '休学';
                }elseif($type == '3'){
                    return '退学';
                }elseif($type == '4'){
                    return '复学';
                }
            });
            $grid->student()->enrollment_time('入学时间')->display(function($enrollment_time){
                return date('Y-m-d',strtotime($enrollment_time));
            });
            $grid->squad_id('原班级')->display(function($squad_id){
                return Squad::find($this->squad_id)->name;
            });
            $grid->trans_squad_id('转入班级')->display(function($trans_squad_id){
                if(is_null($this->trans_squad_id) || $this->trans_squad_id == 0){
                    return '';
                }else{
                    return Squad::find($this->trans_squad_id)->name;
                }
            });
            $grid->op_id('发起人')->display(function($op_id){
                $temp = DB::table('admin_users')->where('id',$op_id)->get(['en_name']);
                foreach($temp as $v){
                    return $v->en_name;
                }
            });
            $grid->created_at('发起时间')->display(function($created_at){
                return date('Y-m-d',strtotime($created_at));
            });
            $grid->sp_id('审批人')->display(function($sp_id){
                $temp = DB::table('admin_users')->where('id',$sp_id)->get(['en_name']);
                foreach($temp as $v){
                    return $v->en_name;
                }
            });
            $grid->sp_time('处理时间')->display(function($sp_time){
                if(is_null($sp_time)){
                    return '';
                }else{
                    return date('Y-m-d',strtotime($sp_time));
                }
            });
            $grid->memo('备注');
            $grid->status('状态')->display(function($status){
                if($status == '0'){
                    return '未审核';
                }elseif($status == '1'){
                    return '审核通过';
                }elseif($status == '2'){
                    return '驳回';
                }
            });

            $grid->actions(function($actions){
                $actions->disableEdit();
                $actions->disableDelete();
                $actions->disableView();
                $sp = "'sp'";
                $back = "'back'";
                $stat = DB::table('applys')->where('id',$actions->getKey())->get();
                foreach($stat as $v){
                    if($v->status == '0'){
                        if(in_array($v->type,['2','3'])){
                            $actions->append('<button class="btn btn-danger" onclick="sp('.$sp.','.$actions->getKey().')">审批</button><button class="btn btn-warning" onclick="sp('.$back.','.$actions->getKey().')">驳回</button>');
                        }else{
                            $actions->append('<button class="btn btn-primary" onclick="trans('.$actions->getKey().')">编辑</button><button class="btn btn-danger" onclick="sp('.$sp.','.$actions->getKey().')">审批</button><button class="btn btn-warning" onclick="sp('.$back.','.$actions->getKey().')">驳回</button>');
                        }
                    }
                }
            });
            $grid->disableExport();
            $grid->disableCreateButton();

            $grid->tools(function($tools){
                $tools->batch(function($batch){
                    $batch->disableDelete();
                });
            });


            $grid->filter(function ($filter) {
                $filter->column(1/2, function ($filter) {
                    $filter->disableIdFilter();
                    $filter->equal('student.no', '学号');
                    $filter->equal('type', '类型')->select(['1'=>'转班','2'=>'休学','3'=>'退学','4'=>'复学']);
                });
                $filter->column(1/2, function ($filter) {
                    $filter->like('student.en_name','英文名');
                    $filter->equal('status', '状态')->select(['0'=>'未审核','1'=>'审核通过','2'=>'驳回']);
                });
            });
        });
    }

    protected function sp_apply_script()
    {
        return <<<SCRIPT

        sp=function(r_type,id){
            var row = $('input[data-id="'+id+'"]').parent().parent().parent();

            var op_type = row.children('td').eq(5).text();
            op_type = op_type.trim();

            var en_name = row.children('td').eq(3).text();
            en_name = en_name.trim();
            var squad = row.children('td').eq(7).text();
            squad = squad.trim();
            var trans_squad = row.children('td').eq(8).text();
            trans_squad = trans_squad.trim();
            var opr = row.children('td').eq(9).text();
            opr = opr.trim();

            var sp_words = '';
            var back_words = '';

            if(op_type == '转班'){
                sp_words = '是否确认将'+squad+'班的'+en_name+'转入'+trans_squad+'班？转班申请通过后学员的信息将正式更变，之后可以在变更记录查询。';
                back_words = '是否确认将'+opr+'发起的'+squad+'班-'+en_name+'转入'+trans_squad+'班的申请驳回？驳回申请后后学员的信息将不会更变，操作之后可以在变更记录中查询驳回记录';
            }else if(op_type == '复学'){
                sp_words = '是否确认将休学的'+en_name+'转入'+trans_squad+'班？复学申请通过后学员的信息将正式更变，之后可以在变更记录查询。';
                back_words = '是否确认将'+opr+'发起的将'+en_name+'转入'+trans_squad+'班的申请驳回？驳回申请后后学员的信息将不会更变，操作之后可以在变更记录中查询驳回记录';
            }else if(op_type == '休学'){
                sp_words = '是否确认将'+squad+'班的'+en_name+'休学？确认操作后将更改数据库中的数据，并且班主任在自己的班级列表中将查询不到该学员，确认操作请在下方输入框中输入“休学”。';
                back_words = '是否确认将'+opr+'发起的将'+en_name+'休学的申请驳回？驳回申请后后学员的信息将不会更变，操作之后可以在变更记录中查询驳回记录';
            }else if(op_type == '退学'){
                sp_words = '是否确认将'+squad+'班的'+en_name+'退学？确认操作后将更改数据库中的数据，并且班主任在自己的班级列表中将查询不到该学员，确认操作请在下方输入框中输入“退学”。';
                back_words = '是否确认将'+opr+'发起的将'+en_name+'退学的申请驳回？驳回申请后后学员的信息将不会更变，操作之后可以在变更记录中查询驳回记录';
            }

            if(r_type == 'sp'){
                content = sp_words;
            }else if(r_type == 'back'){
                content = back_words;
            }

            if(op_type == '转班' || op_type == '复学' || r_type == 'back'){
                 $.confirm({
                    title: '提示信息!',
                    content: content,
                    buttons: {
                        ok: {
                            text: "确定",
                            btnClass: 'btn-danger',
                            keys: ['enter'],
                            action: function(){
                                $.ajax({
                                    method: 'post',
                                    url: 'sp',
                                    data: {
                                        _token:LA.token,
                                        id: id,
                                        type:r_type
                                    },
                                    success: function (data) {
                                        if(data.status > 0){
                                            toastr.success('操作成功');
                                            parent.location.reload();
                                        }else{
                                            toastr.error(data.message);
                                        }
                                    }
                                });
                            }
                        },
                        cancel: {
                            text: "取消",
                            btnClass: 'btn-primary',
                            keys: ['esc'],
                        }
                    }
                });
            }else if(r_type == 'sp' && (op_type=='休学' || op_type=='退学')){
                var value = prompt(content);
                if(value == null){
                    alert('你取消了输入！');
                    return;
                }else if(value != '退学' && op_type == '退学'){
                    alert('输入错误');
                    return;
                }else if(value != '休学' && op_type == '休学'){
                    alert('输入错误');
                    return;
                }else{
                    var request = {id:id};
                    request.type = r_type;
                    $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},async:false});
                    $.post('/admin/apply/sp',request,function(data){
                        if(data.status < 0){
                            alert(data.message);
                            return;
                        }else{
                            window.location.reload();
                            alert('操作成功！');
                        }
                    });
                }
            }
        }
SCRIPT;

    }

    public function delete(Request $request)
    {
        if(isset($request['id']) && $request['id']>0){
            Apply::where('id',$request['id'])->delete();
        }
        return array('status'=>1,'message'=>'操作成功');
    }

    public function update(Request $request)
    {
        $update = Apply::find($request['id']);
        $update->trans_squad_id = $request['squad'];
        $update->memo = $request['memo'];
        $update->save();
        return array('status'=>1,'message'=>'操作成功');
    }

    public function sp(Request $request)
    {
        $update = Apply::find($request['id']);
        if($request['type'] == 'sp'){
            $update->status = '1';

            //add by don at 2019-03-22 start
            //休退学审批成功后，获取学员原班级的教学进度
            $squ_id = Apply::find($request['id'])->squad_id;
            $study_jindu = Squad::find($squ_id)->study_jindu;
            //休退学审批成功后，获取学员所在原班级的人数
            $student_nums = Student::where('squad_id',$squ_id)->where('status','1')->count();

            //获取当前学期信息
            $term = Term::where('status','0')->first();
            //add by don at 2019-03-22 start

            //审批成功直接生效
            $stu = Student::find($update->student_id);
            if($update->type == '1'){
                if($update->trans_squad_id == 0 || $update->trans_squad_id == '' || is_null($update->trans_squad_id)){
                    return array('status'=>-1,'message'=>'请选择转入班级！');
                }
                //转班
                $stu->squad_id = $update->trans_squad_id;
            }elseif($update->type == '2'){
                //add by don at 2019-03-22 start
                $update->study_jindu = $study_jindu;
                $update->student_nums = $student_nums;

                $update->term_year = $term['year'];
                $update->term_season = $term['season'];
                $update->week = ceil((time()-strtotime($term['start_date']))/86400/7);
                //add by don at 2019-03-22 start

                //休学
                $stu->status = '2';

                //休退学的检查reports状态
                DB::table('reports')->where('student_id',$update->student_id)->update(['status'=>1]);
                $pid = DB::table('reports')->where('student_id',$update->student_id)
                    ->orderBy('id','desc')->select('pid')->first();
                $count1 = DB::table('reports')->where('pid',$pid['pid'])->count();
                $count2 = DB::table('reports')->where('status','=',1)->where('pid',$pid['pid'])->count();
                if($count1 == $count2){
                    DB::table('reports')->where('id',$pid['pid'])->update(['status'=>1]);
                }
            }elseif($update->type == '3'){
                //add by don at 2019-03-22 start
                $update->study_jindu = $study_jindu;
                $update->student_nums = $student_nums;

                $update->term_year = $term['year'];
                $update->term_season = $term['season'];
                $update->week = ceil((time()-strtotime($term['start_date']))/86400/7);
                //add by don at 2019-03-22 start
                //退学
                $stu->status = '3';

                //休退学的检查reports状态
                DB::table('reports')->where('student_id',$update->student_id)->update(['status'=>1]);
                $pid = DB::table('reports')->where('student_id',$update->student_id)
                    ->orderBy('id','desc')->select('pid')->first();
                $count1 = DB::table('reports')->where('pid',$pid['pid'])->count();
                $count2 = DB::table('reports')->where('status','=',1)->where('pid',$pid['pid'])->count();
                if($count1 == $count2){
                    DB::table('reports')->where('id',$pid['pid'])->update(['status'=>1]);
                }
            }elseif($update->type == '4'){
                //复学
                $stu->status = '1';
                $stu->squad_id = $update->trans_squad_id;
            }
            $stu->save();
        }elseif($request['type'] == 'back'){
            $update->status = '2';
        }
        $update->sp_id = Admin::user()->id;
        $update->sp_time=date('Y-m-d H:i:s');
        $update->save();
        return array('status'=>1,'message'=>'操作成功');
    }

    public function student_lost_detail( Request $request){
        return Admin::content(function (Content $content) use($request){
            $content->header('学员流失详情');
            $content->description(trans('admin.list'));

            //$student_lost_list = DB::table('apply');
            //$content->body(view('admin.student_lost_detail',compact($request)));
            $content->body($this->student_lost_detail_grid($request));
        });
        //return view('admin.student_lost_detail',compact($request));
    }

    public function student_lost_detail_grid(Request $request){
        $grid = new Grid(new Apply());
            $grid->model()->where(['squad_id'=>$request['squad_id'],'term_year'=>$request['term_year'],'term_season'=>$request['term_season'],'week'=>$request['week']]);
        $grid->paginate(20);
        $grid->student_id('学生信息')->display(function($student_id){
            $student = Student::find($this->student_id);
            return $student->en_name."【".$student->ch_name."】";
        });

        $grid->squad_id('流失前班级')->display(function($squad_id){
            return Squad::find($this->squad_id)->full_name;
        });

        $grid->type('流失类型')->display(function($type){
            if ($this->type==2) {
                return '休学';
            }
            if ($this->type==3) {
                return '退学';
            }
        })->sortable();
        $grid->column('流失时间')->display(function($term_season){
            if ($this->term_season=='1') {
                return "【".$this->term_year."年】【春季学期】【第".$this->week."周】";
            }elseif ($this->term_season=='2') {
                return "【".$this->term_year."年】【秋季学期】【第".$this->week."周】";
            }
        })->sortable();
        $grid->memo('流失原因')->style('max-width:600px;word-break:break-all;');
        $grid->study_jindu('流失时学习进度')->style('max-width:600px;word-break:break-all;');
        $grid->disableCreateButton();
        //$grid->disableExport();
        $filename="流失详情";
        $grid->exporter(new ExcelExpoter($grid,$filename));
        $grid->disableRowSelector();
        $grid->disableActions();
        $grid->disableFilter();

        return $grid;
    }
}
